<template>
  <view class="orders-page">
    <!-- 顶部导航栏 -->
    <view class="navbar">
      <view class="nav-left" @click="goBack">
        <text class="back-icon">←</text>
      </view>
      <view class="nav-title">酒店订单</view>
      <view class="nav-right">
        <text class="search-icon"></text>
      </view>
    </view>

    <!-- 状态标签栏 -->
    <view class="tab-bar">
      <view
        v-for="tab in tabs"
        :key="tab.key"
        :class="['tab-item', { active: currentTab === tab.key }]"
        @click="switchTab(tab.key)"
      >
        {{ tab.name }}
      </view>
    </view>

    <!-- 订单列表 -->
    <scroll-view class="order-list" scroll-y>
      <view v-for="order in filteredOrders" :key="order.id" class="order-item">
        <view class="order-header">
          <image
            class="ticket-icon"
            src="/static/images/hotel.png"
            mode="aspectFit"
          />
          <text class="order-type">酒店</text>
          <text :class="['order-status', statusClass(order.status)]">{{
            order.status
          }}</text>
        </view>

        <view class="order-content">
          <text class="ticket-address">{{ order.address }}</text>
          <text class="ticket-name">{{ order.name }}</text>
          <text class="ticket-date"
            >{{ order.checkInDate }} 到 {{ order.checkOutDate }} ·
          </text>
          <text class="ticket-room">{{ order.roomType }}</text>
          <text class="ticket-price">¥{{ order.totalAmount }}</text>
        </view>

        <view class="order-footer">
          <text class="total-price">¥{{ order.totalAmount }}</text>
          <view class="action-buttons">
            <button
              v-if="order.status === '待付款'"
              class="action-btn primary"
              @click="handlePay(order)"
            >
              去支付
            </button>
            <button
              v-if="order.status === '待付款'"
              class="action-btn secondary"
              @click="handleCancel(order)"
            >
              取消
            </button>
            <button
              v-if="order.status === '未使用'"
              class="action-btn primary"
              @click="handleUse(order)"
            >
              去使用
            </button>
            <button
              v-if="order.status === '已使用'"
              class="action-btn primary"
              @click="handleReview(order)"
            >
              去评价
            </button>
            <button
              v-if="order.status === '已使用' || order.status === '已取消'"
              class="action-btn secondary"
              @click="handleRebuy(order)"
            >
              再次购买
            </button>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import { orderList, update } from "@/api/system/order.js";

export default {
  data() {
    return {
      currentTab: "all",
      tabs: [
        {
          key: "all",
          name: "全部",
        },
        {
          key: "unpaid",
          name: "待付款",
        },
        {
          key: "unused",
          name: "未使用",
        },
        {
          key: "used",
          name: "已使用",
        },
        {
          key: "cancelled",
          name: "已取消",
        },
      ],
      orders: [],
    };
  },
  computed: {
    filteredOrders() {
      if (this.currentTab === "all") return this.orders;
      const statusMap = {
        unpaid: "待付款",
        unused: "未使用",
        cancelled: "已取消",
      };
      return this.orders.filter(
        (order) => order.status === statusMap[this.currentTab]
      );
    },
  },
  created() {
    const id = uni.getStorageSync("userId");
    this.getOrders(id);
  },
  methods: {
    getOrders(id) {
      //获取用户的订单列表
      orderList(id).then((res) => {
        this.orders = res.data;
        //订单状态
        this.orders.forEach((item) => {
          switch (item.status) {
            case "0":
              item.status = "待付款";
              break;
            case "1":
              item.status = "未使用";
              break;
            case "2":
              item.status = "已使用";
              break;
            case "3":
              item.status = "已取消";
              break;
            default:
              item.status = "未知状态";
          }
        });
      });
    },
    statusClass(status) {
      switch (status) {
        case "待付款":
          return "status-pending";
        case "未使用":
          return "status-active";
        case "已使用":
          return "status-used";
        case "已取消":
          return "status-cancelled";
        default:
          return "未知状态";
      }
    },
    switchTab(tab) {
      this.currentTab = tab;
    },
    handlePay(order) {
      uni.navigateTo({
        url: `/pages/pay/payment?orderId=${order.orderId}&totalPrice=${order.totalAmount}`,
      });
    },
    handleCancel(order) {
      uni.showModal({
        title: "提示",
        content: "确定要取消订单吗？",
        success: (res) => {
          if (res.confirm) {
            console.log(order);
            order.status = 3;
            // 用户点击了确定，调用取消订单接口
            update(order).then((res) => {
              if (res.code == 200) {
                uni.showToast({
                  title: "取消成功",
                  icon: "success",
                });
                this.getOrderList();
              } else {
                uni.showToast({
                  title: res.msg || "取消失败",
                  icon: "none",
                });
              }
            });
          }
        },
      });
    },
    handleUse(order) {
      uni.navigateTo({
        url: "/pages/order/order-detail?id=" + order.orderId,
      });
    },
    handleReview(order) {
      uni.navigateTo({
        url: "/pages/order/review?id=" + order.id,
      });
    },
    handleRebuy(order) {
      uni.showToast({
        title: "重新购买功能开发中",
        icon: "none",
      });
    },
    //返回上一级
    goBack() {
      uni.switchTab({ url: "/pages/mine/mine" });
    },
  },
  onLoad(options) {
    // 从预定界面接收订单数据
    if (options.orderData) {
      const newOrder = JSON.parse(decodeURIComponent(options.orderData));
      this.orders.unshift(newOrder);
    }
  },
};
</script>

<style scoped>
.orders-page {
  min-height: 100vh;
  background: #f5f5f5;
}

.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 44px 16px 12px;
  background: #fff;
  z-index: 100;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.nav-left,
.nav-right {
  width: 24px;
}

.back-icon,
.search-icon {
  font-size: 24px;
  color: #333;
}

.nav-title {
  font-size: 18px;
  font-weight: 500;
  color: #333;
}

.tab-bar {
  position: fixed;
  top: 76px;
  left: 0;
  right: 0;
  display: flex;
  background: #fff;
  z-index: 99;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.tab-item {
  flex: 1;
  height: 44px;
  line-height: 44px;
  text-align: center;
  font-size: 14px;
  color: #666;
  position: relative;
}

.tab-item.active {
  color: #333;
  font-weight: 500;
}

.tab-item.active::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 2px;
  background: #007aff;
}

.order-list {
  margin-top: 120px;
  padding: 12px;
  height: calc(100vh - 120px);
}

.order-item {
  background: #fff;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.order-header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  position: relative;
}

.ticket-icon {
  width: 20px;
  height: 20px;
  margin-right: 4px;
}

.order-type {
  font-size: 14px;
  color: #666;
  margin-right: auto;
}

.order-status {
  position: absolute;
  top: 0;
  right: 0;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
}

.status-pending {
  background-color: #e6f4ff;
  color: #1677ff;
}

.status-active {
  background-color: #f6ffed;
  color: #52c41a;
}

.status-used {
  background-color: #f5f5f5;
  color: #b9bfce;
}

.status-cancelled {
  background-color: #eee0e0;
  color: #aea3a3;
}

.order-content {
  border-bottom: 1px solid #f5f5f5;
  padding-bottom: 12px;
  margin-bottom: 12px;
}

.ticket-address,
.ticket-name,
.ticket-date,
.ticket-room,
.ticket-price {
  display: block;
  margin-bottom: 6px;
}

.ticket-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.ticket-date,
.ticket-room {
  font-size: 14px;
  color: #666;
}

.ticket-price {
  font-size: 16px;
  color: #e63946;
  font-weight: bold;
  margin-top: 8px;
}

.order-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.total-price {
  font-size: 18px;
  font-weight: 600;
  color: #e63946;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.action-btn {
  padding: 6px 12px;
  font-size: 14px;
  border-radius: 16px;
}

.action-btn.primary {
  background: #007aff;
  color: #fff;
  border: none;
}

.action-btn.secondary {
  background: #fff;
  border: 1px solid #ddd;
  color: #666;
}
</style>